package com.hkjtgc.pcr_app.adapter

import android.annotation.SuppressLint
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.RecyclerView
import com.hkjtgc.pcr_app.R
import com.hkjtgc.pcr_app.fragment.HomeFragment
import com.hkjtgc.pcr_app.model.TodoInfoDO
import com.hkjtgc.pcr_app.util.DateUtil
import java.util.*

class ToDoListAdapter(
    val homeFragment: HomeFragment,
    private var todoList: List<TodoInfoDO>,
    private val itemClick: ItemClick
) :
    RecyclerView.Adapter<ToDoListAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val listItem: LinearLayout = view.findViewById(R.id.list_item)
        val time: AppCompatTextView = view.findViewById(R.id.tv_title)
        val type: AppCompatTextView = view.findViewById(R.id.tv_bottom)

        val hour: AppCompatTextView = view.findViewById(R.id.hour)
        val begin: AppCompatTextView = view.findViewById(R.id.begin)
        val btnDelete: TextView = view.findViewById(R.id.btnDelete)
        //val btnTop: TextView = view.findViewById(R.id.btnTop)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ToDoListAdapter.ViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_todo_list, parent, false)
        return ViewHolder(view)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    @SuppressLint("SetTextI18n", "SimpleDateFormat")
    override fun onBindViewHolder(holder: ToDoListAdapter.ViewHolder, position: Int) {
        val todoInfo = todoList[position]
        holder.time.text = todoInfo.createDate
        holder.type.text = todoInfo.simpleType

        //todo 时间为 todoInfo.createDate到现在的时间差（取整：差了多少个小时） 超过24小时时holder.hour.text为: ">24H"

        val startTime = todoInfo.createDate
        val endTime =
            DateUtil.formatTimeInMillis(System.currentTimeMillis())
        val time = DateUtil.dateDiff(startTime, endTime, "yyyy-MM-dd HH:mm:ss", "hour")
        if (time > 24) {
            holder.hour.text = ">24H"
        } else {
            holder.hour.text = "${time}H"
        }
        if (homeFragment.start) {
            if (position == 0) {
                holder.begin.text = "进行中"
                holder.begin.setBackgroundResource(R.drawable.btn_get_read)
            } else {
                holder.begin.text = "等待"
                holder.begin.setBackgroundResource(R.drawable.btn_cycle_bg)
            }
        } else {
            holder.begin.text = "开始"
            holder.begin.setBackgroundResource(R.drawable.btn_get_help)
        }


        //开始按钮
        holder.begin.setOnClickListener {
            itemClick.clickStart(position)

        }
        //删除按钮
        holder.btnDelete.setOnClickListener {
            itemClick.clickDelete(position)
        }
        //跳转到数据详情点击事件
        holder.listItem.setOnClickListener {
            itemClick.clickListItem(position)
        }

    }

    interface ItemClick {
        fun clickStart(position: Int)
        fun clickDelete(position: Int)
        fun clickListItem(position: Int)
    }

    override fun getItemCount(): Int = todoList.size

}